package com.example.interceptor;

import com.example.base.DemoContext;
import org.springframework.core.task.TaskDecorator;

public class ContextTaskDecorator implements TaskDecorator {
    @Override
    public Runnable decorate(Runnable runnable) {
        String phone = DemoContext.getPhone();
        String token = DemoContext.getToken();
        return () -> {
            try {
                DemoContext.setToken(token);
                DemoContext.setPhone(phone);
                runnable.run();
            }finally {
                DemoContext.clearToken();
                DemoContext.clearPhone();
                System.out.println("执行完异步任务后，上下文token信息="+DemoContext.getToken());
                System.out.println("执行完异步任务后，上下文手机信息="+DemoContext.getPhone());
            }
        };
    }
}
